// pagesEnterprise/pages/enterprisePreview/enterprisePreview.js

import ajax from '../../../utils/index'
const app = getApp()
const {openMmap} = require("../../../utils/util")
Page({

  /**
   * 页面的初始数据
   */
  data: {
      type: 1, //1认领 2编辑
      detail:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      let that = this
      this.setData({
          type: options.type
      })
      const eventChannel = this.getOpenerEventChannel()
      eventChannel.on('companyFormData', function(data) {
          console.log(data)
          that.setData({
              detail: data.data
          })
      })
  },
  /**
   * 打开地图
   */
  locationEvent() {
      openMmap(this.data.detail.site)
    },
  /**
   * 表单提交
   */
  submit:function(e){
      let that = this

      let form = this.data.detail

      if( that.data.type == 2 ){ //编辑
          ajax.post('/wx/appletCorporateHome/updateHome',form).then( res => {
              if( res.code == 200 ){
                  wx.showToast({
                    title: '提交成功',
                  })
                  setTimeout(() => {
                      wx.navigateBack({
                        delta: 2,
                      })
                  }, 1500);
              }else{
                  wx.showToast({
                      title: res.msg,
                      icon:'none'
                    })
              }
          })
          return
      }

      ajax.post('/wx/appletClaim/add', form).then( res => {
          if( res.code == 200 ){
              wx.showToast({
                title: '认领成功，我们会尽快为您审核，请耐心等待',
                icon:'none'
              })
              setTimeout( () => {
                  wx.navigateBack({
                      delta: 2
                  })
              },1500)
          }else{
              wx.showToast({
                  title: res.msg,
                  icon:'none'
              })
          }
      })
  },

  /**
   * 返回上一页
   */
  back:function(){
      wx.navigateBack({
          delta: 1
      })
  },

})